﻿closures = function()
	local x = { }
	for i = 1, 10 do
		x[i] = function() return i; end
	end
	return x;
end 

local cls = closures(); 

for i = 1, 10 do 
	print(cls[i]()); 
end

--

kernelcomponent = newcomponent {
	name = "kernel",
	id = 1,
	flags = 0,
};

statscomponent = newcomponent {
	name = "stats",
	id = 128,
	flags = 0,
	fields = {
		newfield {
			name = "hp",
			validator = function(host, entity, oldvalue, newvalue)
			                if newvalue <= 0 and oldvalue > 0 then host.sendmsg(entity, newmsg { name = "zerohp" }); end
			            end
		},
	},
	inhandlers = {
		newhandler {	
			msgname = "damage",
			level = handlerlevels.invariant,			
			priority = handlerpriorities.normal,
			handler = function(host, entity, stats, msg) 
					      stats.hp = stats.hp - msg.value					  
					  end
		},
		newhandler {
			msgname = "heal",
			level = handlerlevels.invariant,			
			priority = handlerpriorities.normal,
			handler = function(host, entity, stats, msg)
                          stats.hp = stats.hp + msg.value
			          end			
		},
	},
	outhandlers = {
		
	},	
};

entity = newproto { kernelcomponent }
playerproto = newproto { statscomponent, inheritsfrom = entity }
player = newentity { playerproto }